# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import os.path


class LibgenPipeline:
    def process_item(self, item, spider):
        outfile = item['outfile']
        #  文件保存的路径 ,　并判断文件是否存在
        if os.path.exists(outfile):
            with open(outfile, 'a', encoding='utf-8') as wf:
                wf.write(f"{item['md5']}|{item['id']}|{item['author']}|{item['title']}|{item['language']}\n")
        else:
            with open(outfile, 'w', encoding='utf-8') as wf:
                wf.write(
                    f"md5|id|作者|文章名|语言\n{item['md5']}|{item['id']}|{item['author']}|{item['title']}|{item['language']}\n")
        return item
